package logic

import (
	"context"
	"errors"
	"goods/models/mysql"
	"gorm.io/gorm"

	"goods/goodsrpc/goodsrpc"
	"goods/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PerfectLogic) Perfect(in *goodsrpc.PerfectRequest) (*goodsrpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("id=?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("该用户不存在")
	}
	user = mysql.User{
		Model:     gorm.Model{ID: uint(in.UserId)},
		HeadImage: in.HeadImage,
		NickName:  in.NickName,
	}
	mysql.DB.Debug().Updates(&user)
	return &goodsrpc.PerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
